﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

public partial class Default3 : System.Web.UI.Page
{
    string strCon = System.Configuration.ConfigurationManager.ConnectionStrings["xxnewsConnectionString"].ConnectionString;
    SqlConnection sqlcon;
    SqlCommand sqlcom;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["pn"] == "" || Session["pn"] == null)
        {
            Response.Redirect("default.aspx");
        }
        if (Session["pngeren"] == "" || Session["pngeren"] == null)
        {
            Response.Redirect("default.aspx");
        }
        if (Session["pn"].ToString().Contains("技术科") == false)
        {
            Response.Redirect("default.aspx?lb=审批");
        }
        if (!IsPostBack)
        {
            for (int i = Convert.ToInt32(DateTime.Now.Year); i >= 2011; i--)
            {
                nian.Items.Add(i.ToString());
            }
            for (int i = 1; i <= 12; i++)
            {
                yue.Items.Add(i.ToString());
            }
            nian.SelectedValue = DateTime.Now.Year.ToString();
            yue.SelectedValue = DateTime.Now.Month.ToString();
        }
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#D1DDF1'");
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
            if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
            {
                ((LinkButton)e.Row.Cells[0].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('你确认要删除这条记录吗?删除的同时，也将删除该计划单下已添加的材料列表，确认继续吗？')");
            }
            if (e.Row.Cells[6].Text != DateTime.Now.Year.ToString() || e.Row.Cells[7].Text != DateTime.Now.Month.ToString())
            {
                e.Row.ForeColor = System.Drawing.Color.Tomato;
                e.Row.Cells[13].Text = "已过期计划";
            }
        }
        if (e.Row.RowIndex != -1)
        {
            int id = e.Row.RowIndex + 1;
            e.Row.Cells[1].Text = id.ToString();
        }

    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        try
        {
            string sqlstr2 = "DELETE FROM cailiaomingxi where c_id=" + GridView1.Rows[e.RowIndex].Cells[2].Text;
            //Response.Write(sqlstr2);
            //Response.End();
            sqlcon = new SqlConnection(strCon);
            sqlcom = new SqlCommand(sqlstr2, sqlcon);
            sqlcon.Open();
            sqlcom.ExecuteNonQuery();
            sqlcon.Close();
        }
        catch (SqlException)
        {

            throw;
        }

    }
    protected void GridView1_RowDeleted(object sender, GridViewDeletedEventArgs e)
    {
        string jinggao3 = "<SCRIPT language=javascript>alert('";
        jinggao3 += "删除操作成功！";
        jinggao3 += "');</SCRIPT>";
        ClientScript.RegisterStartupScript(this.GetType(), "aa3", jinggao3);
    }

    protected void DropDownList_sscj_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList_zrbz.Items.Clear();
        DropDownList_zrbz.Items.Add(new ListItem("请选择"));
        DropDownList_zrbz.DataBind();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string sqlstr = @"SELECT     c_id, lei, chejian, banzu, nian, yue, addname, addtime, addip, chejianname, chejiantime, chejianip, duanname, duantime, duanip, cailiaoname, 
                      cailiaotime, cailiaoip, lingdaoname, lingdaotime, lingdaoip, zt, liezhi
FROM         cailiaoguanli
WHERE   1=1 ";
        if (DropDownList_lei.SelectedValue != "请选择")
        {
            sqlstr += " AND (lei = '" + DropDownList_lei.SelectedValue + "')";
        }
        if (nian.SelectedValue != "请选择")
        {
            sqlstr += " AND (nian = '" + nian.SelectedValue + "')";
        }
        if (yue.SelectedValue != "请选择")
        {
            sqlstr += " AND (yue = '" + yue.SelectedValue + "')";
        }
        if (DropDownList_sscj.SelectedValue != "请选择")
        {
            sqlstr += " AND (chejian = '" + DropDownList_sscj.SelectedValue + "')";
        }
        if (DropDownList_zrbz.SelectedValue != "请选择")
        {
            sqlstr += " AND (banzu = '" + DropDownList_zrbz.SelectedValue + "')";
        }
        sqlstr += " AND (zt =2) and (chejian<>'动力设备车间' or shenpibumen='" + Session["pn"].ToString() + "')  ORDER BY [chejiantime] desc";
        SqlDataSource1.SelectCommand = sqlstr;
        GridView1.DataBind();

    }
}